from rest_framework import serializers
from .models import CourseCategory


class CourseCategoryModelSerializer(serializers.ModelSerializer):
    """
    课程分类序列化器
    """

    class Meta:
        model = CourseCategory
        fields = ["id", "name"]


from .models import Course, Teacher


class CourseTeacherModelSerializer(serializers.ModelSerializer):
    """课程所属老师序列化器"""

    class Meta:
        model = Teacher
        fields = ("id", "name", "role", "title", "signature", "brief", "image")


class CourseModelSerializer(serializers.ModelSerializer):
    """
    课程信息序列化器
    """

    teacher = CourseTeacherModelSerializer()  # 老师 1 : 多课程

    # teacher = CourseTeacherModelSerializer(many=True) # 多对1
    class Meta:
        model = Course
        fields = ["id", "name", "students", "lessons", "pub_lessons", "price", "course_img", "teacher", "lesson_list",
                  "discount_name", "real_price"]


class CourseRetrieveSerializer(serializers.ModelSerializer):
    """
    详情页：课程详情信息序列化器
    """
    teacher = CourseTeacherModelSerializer()

    class Meta:
        model = Course
        fields = ["id", "name", "course_img", "students", "lessons", "pub_lessons", "price", "teacher", "brief_html",
                  "level_name", "course_video", "discount_name", "real_price", "activity_time"]


from .models import CourseChapter, CourseLesson


class CourseLessonModelSerializer(serializers.ModelSerializer):
    """
    详情页：课时信息序列化器
    """

    class Meta:
        model = CourseLesson
        fields = ["id", "lesson", "name", "free_trail"]


class CourseChapterModelSerializer(serializers.ModelSerializer):
    """
    详情页：课程章节序列化器
    一个课程章节对多个课时 1:n
    """

    course_sections = CourseLessonModelSerializer(many=True)

    class Meta:
        model = CourseChapter
        fields = ["id", "chapter", "name", "course_sections"]
